/**
 * KopsoxFramework
 * Copyright (C) 2010 Free Software Foundation, Inc. <http://fsf.org/>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.util.validation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.kopsox.comm.message.ErrorMessage;
import org.kopsox.comm.message.ValueMessage;
import org.kopsox.data.ValueDescriptor;
import org.kopsox.environment.proxy.KopsoxRegistry;
import org.kopsox.validation.Validateable;
import org.kopsox.validation.Validator;
import org.kopsox.validation.impl.value.DigitsRangeValidator;
import org.kopsox.validation.impl.value.NotNullValidator;
import org.kopsox.validation.impl.value.ReadonlyValidator;
import org.kopsox.validation.impl.value.TypeValidator;
import org.kopsox.validation.impl.value.ValueRangeValidator;

/**
 * This is a Util-Class for Validators
 * 
 * @author Konrad Renner - 24.06.2010
 *
 */
public final class ValidationUtil {
	
	private ValidationUtil() {
		//Util class
	}

	/**
	 * Runs for each Validateable the notifyValidators-Method<br>
	 * Returns a List of ErrorMessages, if the List is empty, there were no validation errors
	 * @param <T> 
	 * @param validateables
	 * @param message 
	 * @return boolean
	 */
	public static <T extends Validateable> List<ErrorMessage> runValidation(final Collection<T> validateables,final ValueMessage message){
		List<ErrorMessage> ret = new ArrayList<ErrorMessage>();
		for(Validateable v:validateables){
			boolean ok = v.notifyValidators(message);
			
			if(!ok){
				ret.addAll(v.getValidationErrors());
			}
		}
		return ret;
	}
	
	/**
	 * Creates a Set of Validators, which are defined by the ValueDescriptor (NotNull, Readonly, ...)
	 * 
	 * @param descriptor
	 * @return Set<Validator>
	 */
	public static ValidatorSet createValidatorsFromValueDescription(ValueDescriptor descriptor){
		ValidatorSet validators = new ValidatorSet();
		
		//TypeValidator is always active
		validators.add(new TypeValidator());
		
		if(!descriptor.isChangeable()) {
			validators.add(new ReadonlyValidator());
		}
		if(!descriptor.isNullable()) {
			validators.add(new NotNullValidator());
		}
		if(descriptor.getRangeOfValue() != null) {
			validators.add(new ValueRangeValidator());
		}
		if(descriptor.getRangeOfDigitsFromValue() != null) {
			validators.add(new DigitsRangeValidator());
		}
		
		return validators;
	}
	
	/**
	 * Loads the Validator with the given name from the ValidatorEnvironment
	 * 
	 * @param name - String
	 * @return Validator
	 */
	public static Validator getValidatorFromEnvironment(String name) {
		return KopsoxRegistry.INSTANCE.getValidatorEnvironment().getValidator(name);
	}
}
